//
//  WelfareListenViewController.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/22.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class WelfareListenViewController: TTBaseVC {
    
    let WelfareListenTableViewCellID = "WelfareListenTableViewCell"
    
    lazy var listenModel: [WelfareListenModel] = [{
        return WelfareListenModel()
        }()]
    
    private lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - 10), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(WelfareListenTableViewCell.self, forCellReuseIdentifier: WelfareListenTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        tableView.tableHeaderView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 8))
        tableView.setNoData(nodata: true)
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "收听排行榜"
        let bc = UIImageView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP))
        bc.image = IMAGE(name: "welfareListenBack")
        self.view.addSubview(bc)
        self.view.addSubview(tableView)
        tableView.backgroundColor = UIColor.clear
        
        setData()
    }

}

//  MARK: - ###### 方法 ######
extension WelfareListenViewController {
    func setData() {
        MTProgressHUD.show()
        WelfareRequestModel.loadWelfareListenData { (success, mes, model) in
            if success && model != nil {
                self.listenModel = model!
                self.tableView.reloadData()
            }
            
            if self.listenModel.count > 1 {
                self.tableView.setNoData(nodata: false)
            } else {
                self.tableView.setNoData(nodata: true)
            }
            MTProgressHUD.hidded(true)
        }
    }
}

//  MARK: - ###### 协议 ######
extension WelfareListenViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.listenModel.count > 1 {
            return self.listenModel.count
        } else {
            return 0
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 53
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: WelfareListenTableViewCellID) as! WelfareListenTableViewCell
        cell.isUserInteractionEnabled = false
        cell.backgroundColor = UIColor.clear
        if indexPath.row == 0 {
            cell.rankingImage.image = IMAGE(name: "ranking1")
            cell.rankingImage.isHidden = false
            cell.rankingLabel.isHidden = true
        } else if indexPath.row == 1 {
            cell.rankingImage.image = IMAGE(name: "ranking2")
            cell.rankingImage.isHidden = false
            cell.rankingLabel.isHidden = true
        } else if indexPath.row == 2 {
            cell.rankingImage.image = IMAGE(name: "ranking3")
            cell.rankingImage.isHidden = false
            cell.rankingLabel.isHidden = true
        } else {
            cell.rankingImage.isHidden = true
            cell.rankingLabel.isHidden = false
            cell.rankingLabel.text = "\(indexPath.row + 1)"
        }
        let mol:WelfareListenModel = self.listenModel[indexPath.row]
        cell.headImage.sd_setImage(with: URL(string:(StringIncludeChineseToString(mol.imageUrl ?? ""))), placeholderImage: IMAGE(name: "Mine_head"), options: .lowPriority, completed: nil)
        cell.nameLabel.text = mol.wxNickname
        cell.numberLabel.text = String.dateIntervalToSecond(mol.sumTime)
        cell.unitlLable.text = "分钟"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
    }
}
